package com.jy.fxf_phone.application;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.jy.fxf_phone.view.FeedActivity;
import com.liulishuo.filedownloader.FileDownloader;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by JY on 2016/11/18.
 */
public class App extends Application {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;


    private static App ourInstance = new App();

    public static App getInstance() {
        return ourInstance;
    }

    public App() {
    }

    @Override
    public void onCreate() {
        super.onCreate();

        FileDownloader.init(this);

        //首次安装时亲密度默认设置为100
        sharedPreferences = getSharedPreferences("Feed_System", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();

        int intimacy = sharedPreferences.getInt(FeedActivity.SHARED_PREFERENCES_INTIMACY, -1);
        if (intimacy == -1) {
            editor.putInt(FeedActivity.SHARED_PREFERENCES_INTIMACY, 100);
            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_01, 5);
            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_02, 5);
            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_03, 5);
//            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_04, 5);
//            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_05, 5);
//            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_06, 5);
        }
        String lastDate = sharedPreferences.getString(FeedActivity.SHARED_PREFERENCES_DATE, null);
        long currentMills = System.currentTimeMillis();
        Date date = new Date(currentMills);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String today = sdf.format(date);
        Log.d("123", "现在的日期：" + today + " 上次打开的日期:" + lastDate + "是否相等:" + today.equals(lastDate));

        if (!today.equals(lastDate)) {
            int food_01 = sharedPreferences.getInt(FeedActivity.SHARED_PREFERENCES_FOOD_01, 0);
            food_01 += 3;
            editor.putInt(FeedActivity.SHARED_PREFERENCES_FOOD_01, food_01);
            editor.putString(FeedActivity.SHARED_PREFERENCES_DATE, today);
        }
        editor.apply();
    }
}
